<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>注册页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <link rel="stylesheet" href="common.css" />
    <link rel="stylesheet" href="logIn.css" />

    <style>
      #i {
        transition: all 0.618s;
        opacity: 61.8%;
      }
      #i:hover {
        opacity: 100%;
      }
    </style>
  </head>
  <body>
    <div class="navigation">
      <!-- 头像 -->
      <img
        src="https://img1.baidu.com/it/u=4205447136,2730860147&fm=253&fmt=auto&app=138&f=JPEG?w=300&h=300"
        alt="未登录"
      />
      <div class="title">未登录</div>
      <div class="space"></div>

      <a href="login.html" onclick="remove()">登录</a>
    </div>
    <!-- 登录页面 -->

    <div class="login-Container">
      <div class="dialog">
        <h3>注册</h3>

        <form
          target="_blank"
          action="reg"
          method="POST"
          enctype="multipart/form-data"
        >
          <div id="fileImage">
            <input id="i" type="button" value="请上传头像" onclick="judge();" />
            <input id="f_file" type="file" name="file" style="display: none" />
            <div id="conf">
              <input
                type="submit"
                value="确认"
                id="co"
                onclick="alert(
                '如果你需要上传头像，则需要注意以下几点\n' +
                  '1. 点击右侧的确认按钮后，此用户名将无法更改头像！\n' +
                  '2. 用户名应为英文字符，否则虽然注册成功但是头像无法与您进行匹配！\n' +
                  '3. 若用户名已被注册过或者已绑定头像，则无法正常注册！\n' + 
                  '4. 只有填写用户名后才能上传图片，只有上传了图片才能点击确认，否则会出错\n'
              );"
              />
            </div>
          </div>

          <div class="row">
            <label for="username">用户名</label>
            <input type="text" id="username" name="name" />
          </div>
        </form>

        <div class="row">
          <label for="password">密码</label>
          <input type="password" id="password" onclick="getImage()" />
        </div>
        <div class="r">
          <input type="submit" id="submit" value="注册" onclick="reg()" />
        </div>
      </div>
    </div>

    <script>
      function reg() {
        var name = jQuery("#username");
        var password = jQuery("#password");
        if (name.val().trim() == "") {
          alert("清输入名字！");
          name.focus();
          return;
        }
        if (password.val().trim() == "") {
          alert("清输入密码！");
          password.focus();
          return;
        }
        var keyValue = {
          name: name.val(),
          password: password.val(),
        };

        jQuery.ajax({
          type: "post",
          url: "save",
          contentType: "application/json; charset=utf8",
          data: JSON.stringify(keyValue),
          success: function (body) {
            if (body == "No") {
              alert("用户存在");
              name.val("");
              password.val("");
              name.focus();
              jQuery("#i").css(
                "background-image",
                "url(https://img1.baidu.com/it/u=4205447136,2730860147&fm=253&fmt=auto&app=138&f=JPEG?w=300&h=300)"
              );
            } else {
              alert("注册成功");
              window.location.href = "login.html";
            }
          },
        });
      }

      function judge() {
        var name = jQuery("#username");
        var password = jQuery("#password");

        if (name.val().trim() == "") {
          alert("请输入名字");
          name.focus();
          return;
        } else {
          javascript: $("input[name='file']").click();
          jQuery("#i").val("");
        }
      }
      function getImage() {
        var name = jQuery("#username");
        var password = jQuery("#password");

        if (name.val().trim() == "") {
          alert("请输入名字！");
          name.focus();
          return;
        }
        var keyValue = {
          name: name.val(),
          password: password.val(),
        };
        jQuery.ajax({
          type: "post",
          url: "image",
          contentType: "application/json; charset=utf8",
          data: JSON.stringify(keyValue),
          success: function (body) {
            if (body != "No") {
              jQuery("#i").css("background-image", "url(" + body + ")");
              jQuery("#i").val("");
            } else {
              jQuery("#i").css(
                "background-image",
                "url(https://img1.baidu.com/it/u=4205447136,2730860147&fm=253&fmt=auto&app=138&f=JPEG?w=300&h=300)"
              );
              jQuery("#i").val("头像");
            }
          },
        });
      }
    </script>
  </body>
</html>
